package com.yilin.medical.app;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.yilin.medical.activity.BaseActivity;

/**
 * @title
 * @description 用一句话描述该文件做什么
 * @company 北京扎根网络信息技术有限公司
 * @author Jack Zhang
 * @version 1.0
 * @created 2014-12-2 上午11:02:02
 * @changeRecord [修改记录]<br />
 */
public class JxApplication extends Application {

	public static List<String> myFavorites = new ArrayList<String>();

	private static JxApplication mApp;
	private static BaseActivity mContext;

	@Override
	public void onCreate() {
		super.onCreate();
		// 添加crash日志写入本地文件
		// CrashHandlerUtil crashHandler = CrashHandlerUtil.getInstance();
		// crashHandler.init(getApplicationContext());
		mApp = this;
		initImageLoader(this);
	}

	private void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.threadPriority(Thread.NORM_PRIORITY + 5)
				.memoryCache(new LruMemoryCache(12 * 1024 * 1024))
				.memoryCacheSize(12 * 1024 * 1024)
				.discCacheSize(200 * 1024 * 1024)
				.discCacheFileCount(200)
				.discCache(
						new UnlimitedDiscCache(
								getDiskCacheDir(context, "thumb")))
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		ImageLoader.getInstance().init(config);
	}

	public static File getDiskCacheDir(Context context, String uniqueName) {
		String cachePath;
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable()) {
			cachePath = context.getExternalCacheDir().getPath();
		} else {
			cachePath = context.getCacheDir().getPath();
		}
		return new File(cachePath + File.separator + uniqueName);
	}

	public static JxApplication getInstance() {
		return mApp;
	}

	public void exit() {
		// stopService(new Intent(this,UpdateService.class));
		// try {
		// Set<String> set = activityMap.keySet();
		// Iterator<String> iter = set.iterator();
		// while (iter.hasNext()) {
		// String actName = iter.next();
		// Activity currentAct = (Activity) activityMap.get(actName).get();
		// if (currentAct != null) {
		// currentAct.finish();
		// currentAct = null;
		// }
		// }
		// activityMap.clear();
		// activityMap = null;
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// System.exit(0);
		// }
	}

	public static void setApp(JxApplication app) {
		JxApplication.mApp = mApp;
	}

	// 是否联网网络
	public static boolean IsHaveInternet() {
		try {
			ConnectivityManager manger = (ConnectivityManager) mApp
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = manger.getActiveNetworkInfo();
			return (info != null && info.isConnected());
		} catch (Exception e) {
			return false;
		}
	}

	// public void toLocationMap() {
	// getSoufunLocationManager();
	// mSoufunLocationManager.startLocationAndSendMessage();
	// }

}